#coding:utf-8
import requests
import pygal

#请求中国天气万年历网站
url = 'http://wthrcdn.etouch.cn/weather_mini?city=%E4%B8%8A%E6%B5%B7'
r = requests.get(url)
print('status code:', r.status_code)
repo = r.json()
print('keys:',repo.keys())
repo_dicts = repo['data']['forecast']

#将数据储存至列表
dates, highs, lows = [], [], []
for repo_dict in repo_dicts:
    print('\n风向：', repo_dict["fengxiang"])
    print('风力：', repo_dict["fengli"])
    print('最高温度：', repo_dict["high"])
    print('天气：',repo_dict["type"])
    print('最低温度：', repo_dict["low"])
    print('日期：', repo_dict["date"])
    dates.append(repo_dict["date"])
    high = {'value':int(repo_dict["high"][3:5]),
        'label':repo_dict["type"],
        }
    highs.append(high)
    low = {'value':int(repo_dict["low"][3:5]),
        'label':repo_dict["type"],
        }
    lows.append(low)

#设置画布大小及文字大小
my_config = pygal.Config()
my_config.title_font_size = 24
my_config.label_font_size = 14
my_config.major_label_font_size = 18
my_config.width = 1300

#用pygal绘制柱状图  
chart = pygal.Bar(my_config)
chart.title = '天气预报'+str(dates[0])
chart.x_labels = dates
chart.add('最高温度', highs)
chart.add('最低温度', lows)
filename = '天气预报'+str(dates[0])+'.svg'
chart.render_to_file(filename)

